package wp.ggt.com.wpview.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import wp.ggt.com.wpview.R;


/**
 * Created by KF on 2016/3/14.
 */
public class WaterWare extends View {

    private Paint paint;
    private int alpha = 255;
    private int circleX = 100;
    private int circleY = 100;
    private int radius = 0;
    private int width = 10;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            upData();
            postInvalidate();
            if (alpha != 0) {
                handler.sendEmptyMessageDelayed(1, 10);
            } else {
                alpha = 255;
                radius = 0;
            }
        }
    };

    private void upData() {
        alpha -= 10;
        if (alpha < 0) {
            alpha = 0;
        }
        radius += 5;
        paint.setAlpha(alpha);
        paint.setStrokeWidth(radius / 6);
    }

    public WaterWare(Context context) {
        super(context);
    }

    public WaterWare(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WaterWare(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        paint = new Paint();
        paint.setColor(getResources().getColor(R.color.colorAccent));
        paint.setAlpha(alpha);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(width);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(circleX, circleY, radius, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                circleX = (int) event.getRawX();
                circleY = (int) event.getRawY();
                handler.sendEmptyMessage(1);
                break;
        }
        return super.onTouchEvent(event);
    }
}
